package lzyc.pointsManagement.business.honor.service.impl;

import lzyc.pointsManagement.business.approval.domain.ApprovalInfo;
import lzyc.pointsManagement.business.approval.service.ApprovalInfoService;
import lzyc.pointsManagement.business.config.ApprovalStatus;
import lzyc.pointsManagement.business.config.BizType;
import lzyc.pointsManagement.business.honor.domain.HonorInfo;
import lzyc.pointsManagement.business.honor.mapper.HonorInfoMapper;
import lzyc.pointsManagement.business.honor.service.HonorInfoService;
import lzyc.pointsManagement.business.points.domain.PointsInfo;
import lzyc.pointsManagement.business.points.service.PointsInfoService;
import lzyc.pointsManagement.business.service.AttachmentsService;
import lzyc.pointsManagement.business.userdept.service.SysUserDeptService;
import lzyc.pointsManagement.common.core.text.Convert;
import lzyc.pointsManagement.common.utils.DateUtils;
import lzyc.pointsManagement.common.utils.ShiroUtils;
import lzyc.pointsManagement.common.utils.StringUtils;
import lzyc.pointsManagement.common.utils.uuid.IdUtils;
import lzyc.pointsManagement.system.mapper.SysUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * (HonorInfo)表服务实现类
 *
 * @author yangyang
 * @since 2024-03-10 17:03:54
 */
@Service("honorInfoService")
public class HonorInfoServiceImpl implements HonorInfoService {
    @Resource
    private HonorInfoMapper honorInfoMapper;

    @Resource
    private AttachmentsService attachmentsService;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private ApprovalInfoService approvalInfoService;

    @Resource
    private PointsInfoService pointsInfoService;

    @Resource
    private SysUserDeptService sysUserDeptService;

    /**
     * 通过ID查询单条数据
     *
     * @param honorid 主键
     * @return 实例对象
     */
    @Override
    public HonorInfo queryById(String honorid) {
        HonorInfo honorInfo = this.honorInfoMapper.queryById(honorid);
        if (StringUtils.isNotEmpty(honorInfo.getHonorpic())) {
            honorInfo
                .setAttachmentsDetail(Arrays.asList(attachmentsService.getAttachmentsDtoById(honorInfo.getHonorpic())));
        }
        if (StringUtils.isNotEmpty(honorInfo.getApplyuserid())) {
            honorInfo.setApplyuserDetail(
                sysUserDeptService.toSysUserDept(sysUserMapper.selectUserByLoginName(honorInfo.getApplyuserid())));
        }
        return honorInfo;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<HonorInfo> queryAllByLimit(int offset, int limit) {
        List<HonorInfo> honorInfoList = this.honorInfoMapper.queryAllByLimit(offset, limit);
        for (HonorInfo honor : honorInfoList) {
            if (StringUtils.isNotEmpty(honor.getHonorpic())) {
                honor
                    .setAttachmentsDetail(Arrays.asList(attachmentsService.getAttachmentsDtoById(honor.getHonorpic())));
            }
            if (StringUtils.isNotEmpty(honor.getApplyuserid())) {
                honor.setApplyuserDetail(
                    sysUserDeptService.toSysUserDept(sysUserMapper.selectUserByLoginName(honor.getApplyuserid())));
            }
        }
        return honorInfoList;
    }

    /**
     * 根据条件分页查询公告数据
     *
     * @param honorInfo
     * @return
     */
    @Override
    public List<HonorInfo> queryAll(HonorInfo honorInfo) {
        if (!ShiroUtils.isAdmin()){
            honorInfo.setApplyuserid(ShiroUtils.getLoginName());
        }
        List<HonorInfo> honorInfoList = this.honorInfoMapper.queryAll(honorInfo);
        for (HonorInfo honor : honorInfoList) {
            if (StringUtils.isNotEmpty(honor.getHonorpic())) {
                honor
                    .setAttachmentsDetail(Arrays.asList(attachmentsService.getAttachmentsDtoById(honor.getHonorpic())));
            }
            if (StringUtils.isNotEmpty(honor.getApplyuserid())) {
                honor.setApplyuserDetail(
                    sysUserDeptService.toSysUserDept(sysUserMapper.selectUserByLoginName(honor.getApplyuserid())));
            }
        }
        return honorInfoList;
    }

    /**
     * 新增数据
     *
     * @param honorInfo 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(HonorInfo honorInfo) {
        if (StringUtils.isEmpty(honorInfo.getHonorid())) {
            honorInfo.setHonorid(IdUtils.randomUUID());
        }
        if (StringUtils.isEmpty(honorInfo.getApplyuserid())) {
            honorInfo.setApplyuserid(ShiroUtils.getLoginName());
        }
        if (StringUtils.isNull(honorInfo.getApplydate())) {
            honorInfo.setApplydate(DateUtils.getNowDate());
        }
        addAprovalInfo(honorInfo);
        return this.honorInfoMapper.insert(honorInfo);
    }

    /**
     * 通过主键删除数据
     *
     * @param ids 主键
     * @return 是否成功
     */
    @Override
    public int deleteById(String ids) {
        String[] honorIds = Convert.toStrArray(ids);
        deleteApprovalInfo(honorIds);
        deletePointInfo(honorIds);
        return this.honorInfoMapper.deleteById(honorIds);
    }

    /**
     * 批量评审荣誉信息
     *
     * @param ids
     * @param status
     * @return
     */
    @Override
    public int approvalHonorInfos(String ids, String status) {
        String[] honorIds = Convert.toStrArray(ids);
        for (String honorID : honorIds) {
            approvalHonorInfo(honorID, status);
        }
        return honorIds.length;
    }

    /**
     * 评审荣誉
     * 
     * @param honorID
     * @param status
     */
    private void approvalHonorInfo(String honorID, String status) {
        if (status.equals(ApprovalStatus.APPROVED.getCode())) {
            addPointInfo(queryById(honorID));
        }
        approvalInfoService.reviewByProjectId(honorID, status);
    }

    /**
     * 添加评审信息
     *
     * @param honorInfo
     */
    private void addAprovalInfo(HonorInfo honorInfo) {
        ApprovalInfo approvalInfo = initAprovalInfo(honorInfo);
        approvalInfoService.insert(approvalInfo);
    }

    private ApprovalInfo initAprovalInfo(HonorInfo honorInfo) {
        ApprovalInfo approvalInfo = new ApprovalInfo();
        approvalInfo.setProjectId(honorInfo.getHonorid());
        approvalInfo.setType(BizType.Honor.getCode());
        approvalInfo.setStatus(ApprovalStatus.PENDING.getCode());
        approvalInfo.setApplyUserId(honorInfo.getApplyuserid());
        return approvalInfo;
    }

    /**
     * 删除审批信息
     *
     * @param ids
     */
    private void deleteApprovalInfo(String[] ids) {
        approvalInfoService.deleteByProjectId(ids);
    }

    /**
     * 新增积分信息
     * 
     * @param honorInfo
     */
    private void addPointInfo(HonorInfo honorInfo) {
        if (!pointsInfoService.checkPointExist(honorInfo.getHonorid(), honorInfo.getApplyuserid())) {
            pointsInfoService.insert(getHonorPointsInfo(honorInfo));
        }
    }

    private PointsInfo getHonorPointsInfo(HonorInfo honorInfo) {
        PointsInfo pointsInfo = new PointsInfo();
        pointsInfo.setProjectId(honorInfo.getHonorid());
        pointsInfo.setType(BizType.Honor.getCode());
        pointsInfo.setScore(honorInfo.getHonorscore());
        pointsInfo.setPointsUserId(honorInfo.getApplyuserid());
        pointsInfo.setPointsDate(DateUtils.getNowDate());
        return pointsInfo;
    }

    /**
     * 删除积分信息
     *
     * @param ids
     */
    private void deletePointInfo(String[] ids) {
        pointsInfoService.deleteByProjectId(ids);
    }
}